package domain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;

public class DAOContact{

	
	Session session = null;
	private static DAOContact dao=null;
	
	public DAOContact(){				
		session = HibernateUtil.getSessionFactory().openSession();
	}
	
	
	public static DAOContact getInstance(){
		if(dao==null)
			dao=new DAOContact();
		return dao;
	}
	
	
	
	public Contact addContact(String firstname, String lastname, String email){

		try{
		//begin a transaction
		org.hibernate.Transaction tx = session.beginTransaction();
		//create a contact and save it into the DB
		Contact contact = new Contact();
		contact.setFirstName(firstname);
		contact.setLastName(lastname);
		contact.setEmail(email);

		//save the contact into the DB
		session.save(contact); // or session.persist(contact);


		//mandatory to flush the data into the DB
		tx.commit();
		return contact;
		}catch(Exception e){
					System.out.println(e.getMessage());
		}
		return null;
		
	}

	/**
	 * Suppresion d'un contact a partir de son identifiant
	 * @param id
	 * @return vrai si la suppression a bien ete effectuee
	 */
	public int deleteContact(long id){
		
		Contact contact=null;
		int success=0;
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			contact=(Contact) session.get(Contact.class, id);
			session.delete(contact);
			tx.commit();
			success=1;
		} catch( Exception e ){
			e.printStackTrace();
		}
		return success;		
	}

	/**
	 * Recuperation d'un contact a partir de son identifiant
	 * @param id
	 * @return
	 */
	public Contact getContact(long id){
		Contact contact=null;
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			contact=(Contact) session.get(Contact.class, id);
			tx.commit();
		} catch( Exception e ){
			e.printStackTrace();
		}
		return contact;
	}

	
	public boolean modifyContact(long id, String firstname, String lastname, String email){
		boolean success = false;
		Contact contact=null;
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			contact=(Contact) session.get(Contact.class, id);
			contact.setFirstName(firstname);
			contact.setLastName(lastname);
			contact.setEmail(email);
			success = true;
			tx.commit();
		} catch( Exception e ){
			e.printStackTrace();
		}
		return success;		
	}

	
	public List<Contact> getContactByFirstName(String firstname){

		
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			Query q=session.createQuery("from Contact where firstname = :fname");
			q.setString("fname", firstname);
			@SuppressWarnings("unchecked")
			List<Contact> result=q.list();		
			tx.commit();
			return result;
		} catch( Exception e ){
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * Renvoit la liste des contacts correspondant au nom lastname
	 * @param lastname
	 * @return
	 */
	public List<Contact> getContactByLastName(String lastname){
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			Query q=session.createQuery("from Contact where lastname = :lname");
			q.setString("lname", lastname);
			@SuppressWarnings("unchecked")
			List<Contact> result=q.list();		
			tx.commit();
			return result;
		} catch( Exception e ){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Renvoit la liste des contacts correspondant a l'email email
	 * @param email
	 * @return
	 */
	public List<Contact> getContactByEmail(String email){
		try{
			org.hibernate.Transaction tx = session.beginTransaction();
			Query q=session.createQuery("from Contact where email = :email");
			q.setString("email", email);
			@SuppressWarnings("unchecked")
			List<Contact> result=q.list();		
			tx.commit();
			return result;
		} catch( Exception e ){
			e.printStackTrace();
		}
		return null;
	}
	


}
